package cn.kongwg.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author kongwg
 * @create 2021-01-29 21:22
 */
@WebFilter("/admin/*")
public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req= (HttpServletRequest) request;
        Object user = req.getSession().getAttribute("user");
        if (user== null) {
            req.getRequestDispatcher("/login.jsp").forward(req,response);

        } else {

            chain.doFilter(req, response);

        }

    }

    @Override
    public void destroy() {

    }
}
